<?php

class m120501_141722_create_comment extends CDbMigration {

	public function safeUp() {
		$comment = array(
			'id'=>'pk',
			'languageId'=>'integer',
			'userId'=>'integer',
			'name'=>'string',
			'email'=>'varchar(128)',
			'value'=>'text',
			'createdAt'=>'datetime',
			'updatedAt'=>'datetime',
		);
		$this->createTable('comments', $comment);
		$this->createIndex('index_comment_language', 'comments', 'languageId');
		$this->createIndex(
			'index_comment_updated_at', 'comments', 'updatedAt'
		);

		$auth = Yii::app()->authManager;

		$auth->createOperation('commentUpdate', 'Update Comment');
		$auth->createOperation('commentDelete', 'Delete Comment');
		$auth->createOperation('commentManage', 'Manage Comment');

		$role = $auth->createRole('commentModerator', 'Comment Moderator');
		$role->addChild('commentUpdate');
		$role->addChild('commentDelete');
		$role->addChild('commentManage');

		$auth->addItemChild('admin', 'commentModerator');
	}

	public function safeDown() {
		$this->dropTable('comments');

		$auth = Yii::app()->authManager;

		$auth->removeItemChild('admin', 'commentModerator');

		$auth->removeItemChild('commentModerator', 'commentUpdate');
		$auth->removeItemChild('commentModerator', 'commentDelete');
		$auth->removeItemChild('commentModerator', 'commentManage');

		$auth->removeAuthItem('commentUpdate');
		$auth->removeAuthItem('commentDelete');
		$auth->removeAuthItem('commentManage');

		$auth->removeAuthItem('commentModerator');
	}

}